//版权说明：修正按钮、文本位置，版本号：3.0，生成日期：2021-12-26，作者：房晓童，内容：入侵记录页面逻辑
// 功能：实现入侵记录页面的各控件的初始化代码逻辑，与其它文件的关系：activity_break_in_record.xml为其布局
package com.example.safehome;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.ArrayList;
import java.util.List;

public class BreakInRecordActivity extends AppCompatActivity {
    private List<BreakInWarningRecord> recordList = new ArrayList<>();
    private SwipeRefreshLayout swipeRefresh;

    //生成窗口
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.break_in_activity_record);
        initRecord();

        //ListView与工具栏初始化
        BreakInAdapter adapter = new BreakInAdapter(BreakInRecordActivity.this, R.layout.break_in_inresource, recordList);
        ListView listView = (ListView) findViewById(R.id.lv_breakinrecord);
        listView.setAdapter(adapter);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //返回按钮
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    //菜单初始化
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.toolbar,menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case R.id.clear:
                LitePal.deleteAll(BreakInWarningRecord.class);
                initRecord();
                BreakInAdapter adapter = new BreakInAdapter(BreakInRecordActivity.this, R.layout.break_in_inresource, recordList);
                ListView listView = (ListView) findViewById(R.id.lv_breakinrecord);
                listView.setAdapter(adapter);
                break;
            case android.R.id.home:
                finish();
                break;
            default:
                break;
        }
        return true;
    }

    //刷新内容
    private void refreshRecord() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initRecord();
                        BreakInAdapter adapter = new BreakInAdapter(BreakInRecordActivity.this, R.layout.break_in_inresource, recordList);
                        adapter.notifyDataSetChanged();
                        swipeRefresh.setRefreshing(false);
                    }
                });
            }
        }).start();
    }

    //非法入侵报警记录onStart
    @Override
    protected void onStart() {
        super.onStart();
        initRecord();
        BreakInAdapter adapter = new BreakInAdapter(BreakInRecordActivity.this, R.layout.break_in_inresource, recordList);
        ListView listView = (ListView) findViewById(R.id.lv_breakinrecord);
        listView.setAdapter(adapter);
    }

    //非法入侵报警记录onResume
    @Override
    protected void onResume() {
        super.onResume();
        initRecord();
        BreakInAdapter adapter = new BreakInAdapter(BreakInRecordActivity.this, R.layout.break_in_inresource, recordList);
        ListView listView = (ListView) findViewById(R.id.lv_breakinrecord);
        listView.setAdapter(adapter);
    }

    //非法入侵报警记录onRestart
    @Override
    protected void onRestart() {
        super.onRestart();
        initRecord();
        BreakInAdapter adapter = new BreakInAdapter(BreakInRecordActivity.this, R.layout.break_in_inresource, recordList);
        ListView listView = (ListView) findViewById(R.id.lv_breakinrecord);
        listView.setAdapter(adapter);
    }

    //初始化入侵记录展示
    private void initRecord() {
        List<BreakInWarningRecord> records = LitePal.findAll(BreakInWarningRecord.class);
        recordList.clear();
        for (BreakInWarningRecord record : records) {
            recordList.add(record);
        }

    }
}
